package com.systop.common.controller;
import java.io.File;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {
	
	@RequestMapping(value="/upload",method=RequestMethod.GET)
	public String upload() {
		return "upload";
	}
	
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(String name,MultipartFile uploadfile,HttpServletRequest request) {
		//文件的原始名称
		String originalFilename = uploadfile.getOriginalFilename();
		//查找项目中是否有upload路径
		String path = request.getServletContext().getRealPath("/upload");
		//新建file文件
		File filepath = new File(path);
		if(!filepath.exists()) {
			filepath.mkdirs();
		}
		//生成新的名字
		String newFileName = name+"_"+UUID.randomUUID() + "_"+originalFilename;
		try {
			//ʹ//使用MultipartFile类transferTo方法上传参数是个文件filepath+newFileName==/upload/xxx_1.txt
			uploadfile.transferTo(new File(filepath+"/"+newFileName));
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
}
